using System;
using MSOffice = Microsoft.Office.Core;

namespace Office.Utility.Toolbar
{
    public sealed class ToolbarButton : ToolbarControl
	{
        private MSOffice.MsoButtonStyle _style = MSOffice.MsoButtonStyle.msoButtonCaption;
        private int? _faceId;
        private bool? _enabled;
		public event EventHandler<CommandBarEventArgs> Click;
        private bool? _visible;

		public MSOffice.MsoButtonStyle Style
		{
			get { return _style; }
			set { _style = value; }
		}

		public ToolbarButton(string id)
			: base(id)
		{
		}

		public ToolbarButton(string id, string caption)
			: base(id, caption)
		{
		}

		public ToolbarButton(string id, string caption, string tooltip)
			: base(id, caption, tooltip)
		{
		}

		public ToolbarButton(string id, string caption, MSOffice.MsoButtonStyle style)
			: base(id, caption)
		{
			_style = style;
		}

		public ToolbarButton(string id, string caption, string tooltip, MSOffice.MsoButtonStyle style)
			: base(id, caption, tooltip)
		{
			_style = style;
		}

        public ToolbarButton(string id, string caption, string tooltip, MSOffice.MsoButtonStyle style, int faceId)
            : base(id, caption, tooltip)
        {
            _style = style;
            _faceId = faceId;
        }

        public ToolbarButton(string id, string caption, string tooltip, MSOffice.MsoButtonStyle style, bool enabled)
            : base(id, caption, tooltip)
        {
            _style = style;
            _enabled = enabled;
        }

        public ToolbarButton(string id, string caption, string tooltip, MSOffice.MsoButtonStyle style, int faceId, bool enabled)
            : base(id, caption, tooltip)
        {
            _style = style;
            _faceId = faceId;
            _enabled = enabled;
        }

        public ToolbarButton(string id, string caption, string tooltip, MSOffice.MsoButtonStyle style, int faceId, bool enabled, bool visible)
            : base(id, caption, tooltip)
        {
            _style = style;
            _faceId = faceId;
            _enabled = enabled;
            _visible = visible;
        }

		internal override void Attach(MSOffice.CommandBar bar, WindowAdapter window)
		{
			var btn = FindControl<MSOffice.CommandBarButton>(bar);

			if (btn == null)
			{
				btn = (MSOffice.CommandBarButton)bar.Controls.Add(
					MSOffice.MsoControlType.msoControlButton,
					Type.Missing, Type.Missing,
					Position != null ? Position.Value : Type.Missing, true);

				btn.Parameter = Id;
				btn.Tag = Guid.NewGuid().ToString();
			}

			btn.Caption = Caption;
			btn.TooltipText = ToolTip;
			btn.Style = Style;
            if (_faceId != null)
                btn.FaceId = _faceId.Value;
            if (_enabled != null)
                btn.Enabled = _enabled.Value;
            if (_visible != null)
                btn.Visible = _visible.Value;

			btn.Click += delegate
			                 {
				var args = new CommandBarEventArgs(window, bar);
                //Use CommandBarManager's DynamicInvoke to ensure this event fires on the right thread
                CommandBarManager.DynamicInvoke(Click, this, args);
			};

			AddControl(btn);
			window.Close += ((sender, args) => RemoveControl(btn));
		}
	}
}
